//
//  YYLanguageView.m
//  CollarMicrophone
//
//  Created by Yown on 2024/4/15.
//

#import "YYLanguageView.h"
#import "YYMineCell.h"

#import <BIProjectModule/BIProjectContext.h>
#import <BIToolModule/BIProjectDefine.h>
#import <BIRouteModule/BIRouteManager.h>
#import <BIProjectModule/BITableView.h>
#import <BIToolModule/BILanguage.h>
#import <BIToolModule/BIUserDefault.h>

#import <BIProjectModule/BIAppDefine.h>
#import <BIToolModule/UIView+Custom.h>
#import <BIToolModule/NSArray+Helper.h>
#import <BIToolModule/NSObject+ViewController.h>

@interface YYLanguageView () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet BITableView *tableView;
@property (nonatomic, copy) NSArray *cellArray;

@end

@implementation YYLanguageView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = BIColor(BIAppMainBGColor);
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.bounces = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"YYMineCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:[YYMineCell cellIdentifier]];
}

- (void)reloadView:(NSArray *)cellArray {
    self.cellArray = cellArray;
    [self.tableView reloadData];
}

#pragma mark - UITableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [YYMineCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YYMineCellModel *model = [self.cellArray safeObjectAtIndex:indexPath.row];
    YYMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[YYMineCell cellIdentifier] forIndexPath:indexPath];
    [cell setupModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellArray enumerateObjectsUsingBlock:^(YYMineCellModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == indexPath.row) {
            obj.arrowName = @"language_selected";
            if ([obj.jumpCmd isEqualToString:@"system"]) {
                [[BILanguage sharedInstance] setSystemLanguage];
            } else {
                [[BILanguage sharedInstance] setLanguage:obj.jumpCmd];
            }
            [[BIUserDefault sharedInstance] setObject:@(idx) key:@"YYLanguageSettingIndex"];
        } else {
            obj.arrowName = @"";
        }
    }];
    [tableView reloadData];
}

@end
